package company.maestro;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * AddStudentActivity allows the user to add student
 * and guardian information for a new student.
 * @author Maestro
 *
 */
public class AddStudentActivity extends Activity
{
	private Button 				btnAddStudent;
	private Button				btnTakePicture;
	private StudentDBAdapter 	database;
	private EditText 			txtStudentName;
	private EditText 			txtGuardianName;
	private EditText 			txtGuardianPhone;
	private EditText 			txtGuardianEmail;
	private ImageView			imgStudentPicture;
	private Bitmap				studentPictureBitmap;
	private	 long				classId;
	
	private static final int REQUEST_IMAGE = 100;
	
	/**
	 * Called when the activity is launched.
	 */
	public void onCreate(Bundle icicle) 
	{
	    super.onCreate(icicle);
	    setContentView(R.layout.add_student);
	    
	    Bundle extras = getIntent().getExtras(); 
	    
	    classId = -1;
	    if(extras !=null) 
	    {
	    	
	    }
	    
	    database = StudentDBAdapter.getInstance(this);
	    
	    // Initialize UI forms in the Activity
	    btnAddStudent 		= (Button)findViewById(R.id.btnAddStudent);
	    btnTakePicture		= (Button)findViewById(R.id.btnAddPhoto);
	    txtStudentName 		= (EditText)findViewById(R.id.txtStudentName);
	    txtGuardianName 	= (EditText)findViewById(R.id.txtGuardianName);
	    txtGuardianPhone 	= (EditText)findViewById(R.id.txtGuardianPhone);
	    txtGuardianEmail 	= (EditText)findViewById(R.id.txtGuardianEmail);	
	    imgStudentPicture	= (ImageView)findViewById(R.id.imgStudentPicture);
	    
	    
	    // Listener for the add student button.  When the button is clicked,
	    // add the student information to the database and finish the
	    // activity.
	    btnAddStudent.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) 
			{
				// Check to make sure there are two parts to the name.
				// If there aren't, just default to using only the
				// first part (useful if the field is left blank, 
				// for instance - this will need to be fixed later
				// to not allow a blank name).
				Student student = null;
				String[] nameParts = txtStudentName.getText().toString().split(" ");
				if ( nameParts.length == 2 )
				{
					String firstName = nameParts[0];
					String lastName  = nameParts[1];
					student = new Student(firstName, lastName);
				}
				else
				{
					String firstName = nameParts[0];
					student = new Student(firstName, "");
				}
				
				String guardianName		= txtGuardianName.getText().toString();
				String guardianPhone 	= txtGuardianPhone.getText().toString();
				String guardianEmail 	= txtGuardianEmail.getText().toString();
				student.setGuardianName(guardianName);
				student.setGuardianPhone(guardianPhone);
				student.setGuardianEmail(guardianEmail);
				student.setClassId(classId);
				
				database.open();
				long insertId = database.insertStudent(student);
				database.saveStudentImage(insertId, studentPictureBitmap);
				database.close();
				
				setResult(RESULT_OK);

				finish();
			}

		});
	    
	    
	    
	    
	    // Listener for the take picture button.  When the button is
	    // pressed, pass control to the camera.  The result is handled
	    // in onActivityResult
	    btnTakePicture.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) 
			{
				Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);   
		        startActivityForResult(camera, REQUEST_IMAGE);

			}
	    });
	    
	    
	    
	    
	}
	
	@Override
	/**
	 *  Handles the results of activities launched by this Activity.
	 *  In this case, we're only handling the result returned to
	 *  us by the camera activity.
	 */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		if (requestCode == REQUEST_IMAGE)
            if (resultCode == Activity.RESULT_OK) {
                
            	// Display image received on the view
                Bundle b = data.getExtras();
                Bitmap pic = (Bitmap) b.get("data");

                if (pic != null) {
                    imgStudentPicture.setImageBitmap(pic);
                    studentPictureBitmap = pic;
                    imgStudentPicture.invalidate();
                }
            }
            else if (resultCode == Activity.RESULT_CANCELED) 
            {
            	// Do nothing
            }
   }

    
}
